package com.caineng.zhaobiao.zbapi.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import com.caineng.zhaobiao.zbapi.dao.CaiFeedbackDao;
import com.caineng.zhaobiao.zbapi.domain.CaiBaseUser;
import com.caineng.zhaobiao.zbapi.domain.CaiFeedback;
import com.caineng.zhaobiao.zbapi.service.UserService;
import com.caineng.zhaobiao.zbapi.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 反馈举报
 * @author liandyao
 * @date 2022/8/2 9:14
 */
@Slf4j
@Api(tags = "举报反馈服务")
@RestController
@RequestMapping("/zbapi/feedback")
public class CaiFeedbackController {

    @Autowired
    CaiFeedbackDao feedbackDao ;

    @Autowired
    UserService userService;

    @PostMapping("/addFeedback")
    @ApiOperation(value = "举报反馈")
    public AjaxResult addFeedback(CaiFeedback feedback, HttpServletRequest request) {


        if(StpUtil.isLogin()){
            String userId = StpUtil.getLoginIdAsString();
            feedback.setUserId(userId);
            CaiBaseUser userPo = userService.findUserById(userId);
            feedback.setUserPhone(userPo.getPhone());
        }
        String platform = request.getHeader("platform");
        String userAgent = request.getHeader("user-agent");
        feedback.setUserAgent(platform+"||"+userAgent);

        feedback.setCreateTime(DateUtil.now());

        feedbackDao.insert(feedback);
        return AjaxResult.success("成功");
    }

    @PostMapping("/findFeedback")
    @ApiOperation(value = "举报反馈记录查看")
    public AjaxResult findFeedback() {
        CaiFeedback feedback = new CaiFeedback();
        String userId = StpUtil.getLoginIdAsString();
        feedback.setUserId(userId);
        List<CaiFeedback> list = feedbackDao.findAll(Example.of(feedback), Sort.by(Sort.Order.desc("createTime")));
        return AjaxResult.success("成功",list);
    }
}
